
<template>
  <div class="shoppingcart">
    <van-checkbox-group v-model="result" ref="checkboxGroup">
      <van-swipe-cell >
        <van-row type="flex" justify="center">
          <van-col span="2">
            <van-checkbox v-model="result" @click="change"></van-checkbox>
          </van-col>
          <van-col span="22">
            <van-card
              :price="item.minPrice"
              :desc="item.characteristic"
              :title="item.name"
              :thumb="item.pic"
              v-for="(item, index) in list"
              :key="index"
            ></van-card>
          </van-col>
        </van-row>
        <template #right>
          <van-button
            square
            text="删除"
            type="danger"
            class="delete-button"
            @click="remove(item.basicInfo.id)"
          />
        </template>
      </van-swipe-cell>
    </van-checkbox-group>
    <div class="shopping_buont">
      <van-checkbox v-model="checked" @click="quanx">全选</van-checkbox>
      <div>{{ sum }}</div>
      <div>
        <span v-show="edit">下单</span>
        <span v-show="!edit">删除</span>
      </div>
    </div>
    <div class="like">
      <div>猜你喜欢</div>
      <ul>
        <li
          v-for="(item, index) in shopList"
          :key="index"
          v-show="index >= random && index < random + 4"
          @click="goToDetail(item.id)"
        >
          <img :src="item.pic" alt="" />
          <div>
            <p>{{ item.name }}</p>
            <p>{{ item.name }}</p>
            <p>
              <span>￥{{ item.minPrice }}</span
              ><span>已售{{ item.stores }}</span>
            </p>
          </div>
        </li>
      </ul>
    </div>
  </div>
</template>
<script>
import mixin from "../../store/index";
export default {
  mixins: [mixin],
  data() {
    return {
      //   list: [],
      checked: false,
      shopList: [],
      random: "",
      edit: true,
      result: "",
    };
  },
  mounted() {
    // this.list = this.list;
    // console.log(this.list);
    this.$API
      .getStoreList()
      .then((res) => {
        this.shopList = res.data.data;
        this.random = Math.floor(
          Math.random() * (this.shopList.length - 4 - 5 + 1) + 5
        );
        console.log(this.random);
        console.log(this.shopList);
      })
      .catch((err) => {
        console.log(err);
      });
  },
  methods: {
    goToDetail(id) {
      this.$router.push({ path: "/details", query: { id } });
    },
    //   数量加
    plus(id) {
      this.SHOPPINGCART_QLUS(id);
    },
    //   数量减
    minus(id) {
      this.SHOPPINGCART_MINUS(id);
    },
    change() {
      if (this.detailslist.length == this.list.length) {
        this.chdcked = true;
      } else {
        this.chdcked = false;
      }
    },
    quanx() {
      this.$refs.checkboxGroup.toggleAll(this.chdcked);
    },
    remove(id) {
      this.ShoppingCart_remove(id);
    },
    // 编辑
    onedit() {
      if (this.edit === false) {
        this.edit = true;
      } else {
        this.edit = false;
      }
    },
  },
  computed: {
    sum() {
      let res = 0;
      this.list.forEach((item) => {
        res += item.num * item.basicInfo.minPrice;
      });
      return res;
    },
  },
};
</script>
<style lang="scss">
.shoppingcart {
  margin-top: 1rem;
  margin-bottom: 2rem;

  .van-col--2 {
    display: flex;
    justify-content: space-around;
    align-items: center;
  }
  .van-button--square {
    height: 100%;
  }
  .shopping_buont {
    position: fixed;
    z-index: 999999999;
    width: 100%;
    height: 1rem;
    background: #fff;
    bottom: 0.9rem;
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-size: 0.3rem;
    > div {
      &:nth-child(1) {
        width: 20%;
        display: flex;
        justify-content: space-around;
        align-items: center;
      }
      &:nth-child(3) {
        width: 25%;
        height: 100%;
        display: flex;
        justify-content: space-around;
        align-items: center;
        background: darkred;
        color: #fff;
      }
    }
  }
  .like {
    > div {
      display: flex;
      align-items: center;
      justify-content: center;
      height: 0.8rem;
      margin: 0.15rem 0;
      font-size: 0.3rem;
    }
    > ul {
      width: 100%;
      display: flex;
      flex-wrap: wrap;
      align-content: start;
      justify-content: flex-start;
      li {
        width: 45%;
        padding: 0.05rem 0;
        margin-left: 3.33%;
        position: relative;
        img {
          height: 3rem;
          width: 100%;
        }

        p {
          width: 80%;
          white-space: nowrap;
          text-overflow: ellipsis;
          overflow: hidden;
          &:nth-child(1) {
            color: black;
            font-size: 0.28rem;
          }
          &:nth-child(2) {
            color: rgb(240, 162, 73);
            width: 100%;
            height: 0.5rem;
            line-height: 0.5rem;
            position: absolute;
            top: 2.6rem;
            left: 0;
            background-color: khaki;
            font-size: 0.2rem;
            padding-left: 4%;
            box-sizing: border-box;
          }
          &:nth-child(3) {
            width: 100%;
            box-sizing: border-box;
            padding: 0 0.05rem;
            margin-top: 0.05rem;
            display: flex;
            align-items: center;
            justify-content: space-between;
            span {
              &:first-child {
                color: firebrick;
                font-size: 0.3rem;
              }
              &:last-child {
                color: #aaa;
                font-size: 0.3rem;
              }
            }
          }
        }
      }
    }
  }
}
</style>
